const { getPool } = require('../config/db')

class Role {
  static async create(role_name, role_description) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('INSERT INTO user_roles (role_name, role_description) VALUES (?,?)', [role_name, role_description])
    return rows.insertId
  }

  static async update(role_name, role_description, role_id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('UPDATE menus SET role_name =?, role_description =? WHERE role_id =?', [role_name, role_description, role_id])

    return rows
  }

  static async deleteById(roleId) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('DELETE FROM user_roles WHERE role_id = ?', [roleId])

    return rows
  }

  static async findById(roleId) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM user_roles WHERE role_id =?', [roleId])
    return rows[0]
  }

  static async findAll() {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM user_roles', [])
    return rows
  }
}

module.exports = Role
